//
//  ChildCell.m
//  yixintong8127
//
//  Created by longzhiyuan on 15/5/5.
//
//

#import "ChildCell.h"
#import "ASIHTTPRequest.h"
#import "iToast.h"
#import "GlobalVar.h"
#import "MBProgressHUD.h"
#import "ASIFormDataRequest.h"
//
@implementation ChildCell

- (void)awakeFromNib {
    // Initialization code
}
-(BOOL)isSelected
{
  
    
    NSString *nsStrPhone = [GlobalVar loginUserKey];
    //type 类型，比如：中国移动、中国电信、中国联通，分别是：0,1,2
    NSString *nsStrTypeName;
    switch ([GlobalVar getTypeByPhoneNumber])
    {
        case 0:
            nsStrTypeName = @"中国移动";
            break;
        case 1:
            nsStrTypeName = @"中国电信";
            break;
        case 2:
            nsStrTypeName = @"中国联通";
            break;
    }
    //
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示：" message:@"" delegate:self cancelButtonTitle:@"现在兑换" otherButtonTitles:@"取消", nil];
    alertView.message = [NSString stringWithFormat:@"%@\n%@\n%@",nsStrTypeName,nsStrPhone,self.info];
    [alertView show];
    return [super isSelected];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    if (buttonIndex == 0)
    {
        //NSLog(@"alertView.tag===11111==%@",self.itemId);
        [self huafeiChangeLiuliang:self.itemId];
    }
}
#pragma mark 话费换流量
-(void)huafeiChangeLiuliang:(NSString *)flowId;
{
    if(self.delegate)
    {
        [self.delegate childInputCellQequest:@"正在充流量,请稍候..."];
    }
    
    NSString* preMd5=[NSString stringWithFormat:@"%@%@%@%@%@",[GlobalVar loginUserKey],[GlobalVar loginUserKey],flowId,SOFT_ID,@"ysw"];
    //NSString* preMd5=[NSString stringWithFormat:@"%@%@%@%@%@",[GlobalVar loginUserKey],[GlobalVar loginUserKey],[[NSUserDefaults standardUserDefaults] objectForKey:@"flowId"],SOFT_ID,@"ysw"];
    NSString* md5=[GlobalVar md5Str:preMd5];
    /*
    HUD = [[[MBProgressHUD alloc] initWithView:self]autorelease];
    HUD.removeFromSuperViewOnHide = YES;
    [self addSubview:HUD];
    [HUD setLabelText:@"正在充流量,请稍候..."];
    [HUD show:YES];
     */
    //
    ASIFormDataRequest* request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://61.146.138.146:8008/lyb/tel/pay.php"]];
    [request setTimeOutSeconds:TIME_OUT_SECONDS];
    [request setDelegate:self];
    [request setPostValue:[GlobalVar loginUserKey] forKey:@"mobile"];
    [request setPostValue:[GlobalVar loginUserKey] forKey:@"paytel"];
    [request setPostValue:SOFT_ID forKey:@"softId"];
    //[request setPostValue:[[NSUserDefaults standardUserDefaults] objectForKey:@"flowId"] forKey:@"flowID"];
    [request setPostValue:flowId forKey:@"flowID"];
    [request setPostValue:md5 forKey:@"md5"];
    /*
    NSLog(@"mobile ==:%@ \n",[GlobalVar loginUserKey]);
    NSLog(@"paytel ==:%@ \n",[GlobalVar loginUserKey]);
    NSLog(@"softId ==:%@ \n",SOFT_ID);
    NSLog(@"flowID ==:%@ \n",flowId);
    NSLog(@"url ==:%@ \n",request.url);
    */
    [request setDidFinishSelector:@selector(huafeiChangeLiuliangSuccess:)];
    [request setDidFailSelector:@selector(huafeiChangeLiuliangFail:)];
    [request startAsynchronous];
}

//话费换流量回调
-(void)huafeiChangeLiuliangSuccess:(ASIHTTPRequest*)request
{
    if (self.delegate)
    {
        [self.delegate childInputCellQequestSuccess];
    }
    //[HUD hide:YES];
    NSString* changeInfo=[request responseString];
    NSString* msg=[[changeInfo componentsSeparatedByString:@"|"] objectAtIndex:1];
    iToast* toast=[iToast makeText:msg];
    [toast show];
}
-(void)huafeiChangeLiuliangFail:(ASIHTTPRequest*)request
{
    if (self.delegate)
    {
        [self.delegate childInputCellQequestFail];
    }
    iToast* toast=[iToast makeText:ERRORCODE];
    [toast show];
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (void)dealloc {
    [_lab release];
    [super dealloc];
}
@end
